﻿package src.com.online.rpg
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author Mike Welsh
	 */
	public class FPSTimer extends Sprite
	{
		private var _fpsText:TextField;
		private var _frameTime:uint;
		private var _frames:uint;
		private var _time:uint;
		
		private const UPDATE_INTERVAL:uint = 60;
		
		private var _fps:Number = 0;
		
		[Embed(source="FFFHARMO.TTF", fontName="FFF Harmony", mimeType="application/x-font")]
		private static const Font:Class;
		
		public function FPSTimer() 
		{
			_frameTime = 0;
			_frames = 0;

			_fpsText = new TextField();
			_fpsText.autoSize = TextFieldAutoSize.LEFT;
			_fpsText.embedFonts = true;
			_fpsText.selectable = false;
			
			var textFormat:TextFormat = new TextFormat;
			textFormat.align = TextFormatAlign.LEFT;
			textFormat.color = 0xffffff;
			textFormat.font = "FFF Harmony";
			
			_fpsText.defaultTextFormat = textFormat;
			_fpsText.filters = [new DropShadowFilter(0, 0, 0, 1, 2, 2, 255)];
			_fpsText.text = "0 FPS";
			addChild(_fpsText);
						
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			_time = getTimer();
		}
		
		public function onEnterFrame(e:Event):void
		{
			// calculate how much time has passed
			var dt:uint = getTimer() - _time;
			_time += dt;
			
			// calculate FPS at regular intervals
			_frameTime += dt;
			_frames++;
			if (_frames >= UPDATE_INTERVAL)
			{
				_fps = UPDATE_INTERVAL  * 1000 / _frameTime;
				_fpsText.text = (uint(_fps * 100) / 100).toString() + " FPS";
				_frames = 0;
				_frameTime = 0;
			}
		}
		
		public function get fps():Number			{ return _fps; }
		
	}

}